package com.notepad.util;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.notepad.adapter.HomeListAdapter;
import com.notepad.common.ProtocolElement;
import com.notepad.common.RequestData;
import com.notepad.common.ResponseData;
import com.notepad.domain.Notepad;
import com.notepad.ui.EmptyViewTipActivity;
import com.notepad.ui.MainActivity;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HandshakeCompletedListener;

/**
 * Created by jiamengyu on 1/7/2015.
 */
public class DataUtil implements DataUtilInterface {
    private static final String TAG = "responseData";
    private static final String URL = "http://tibby.aliapp.com/note";
    private RequestQueue mRequestQueue;
    private  Handler mHandler;

    public DataUtil(Context context){
        mRequestQueue = Volley.newRequestQueue(context);
    }

    public DataUtil(Context context,Handler handler){
        mRequestQueue = Volley.newRequestQueue(context);
        this.mHandler = handler;
    }



    @Override
    public void insertNote(Notepad notepad) {
        RequestData requestData = new RequestData();
        requestData.setCommand(0);
        requestData.getExtra().put(ProtocolElement.NOTEPAD, JSON.toJSONString(notepad));
        Map<String,String> map = new HashMap<>();
        map.put(ProtocolElement.REQUEST_DATA,JSON.toJSONString(requestData));
        StringRequest insertRequest = new StringRequest(URL,map,new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG,response.replace("\\",""));
            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG,error.getMessage());
            }
        });
        mRequestQueue.add(insertRequest);
    }

    @Override
    public void deleteNote(Notepad note) {
        RequestData requestData = new RequestData();
        requestData.setCommand(1);
        requestData.getExtra().put(ProtocolElement.NOTEPAD,JSON.toJSONString(note));
        Map<String,String> map = new HashMap<>();
        map.put(ProtocolElement.REQUEST_DATA,JSON.toJSONString(requestData));
        StringRequest deleteRequest = new StringRequest(URL,map,new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG,response.replace("\\",""));
                mHandler.sendEmptyMessage(1);
            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG,error.getMessage());
            }
        });
        mRequestQueue.add(deleteRequest);
    }

    @Override
    public void updateNote(Notepad notepad) {
        RequestData requestData = new RequestData();
        requestData.setCommand(2);
        requestData.getExtra().put(ProtocolElement.NOTEPAD,JSON.toJSONString(notepad));
        Map<String,String> map = new HashMap<>();
        map.put(ProtocolElement.REQUEST_DATA, JSON.toJSONString(requestData));
        StringRequest updateRequest = new StringRequest(URL,map,new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG,response.replace("\\",""));
            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG,error.getMessage());
            }
        });
        mRequestQueue.add(updateRequest);
    }

    @Override
    public void findAllNote() {

        final RequestData requestData = new RequestData();
        requestData.setCommand(3);
        Map<String,String> map = new HashMap<>();
        map.put(ProtocolElement.REQUEST_DATA,JSON.toJSONString(requestData));
        StringRequest findRequest = new StringRequest(URL,map,new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG,response.replace("\\",""));
            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG,error.getMessage());
            }
        });
        mRequestQueue.add(findRequest);
    }


}
